﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OpenXmlEasy.Word
{
    public partial class WordEasy
    {

        public void InsertImage(Image img, string imageName = null)
        {
            _imageCount++;
            var mainPart = _doc.MainDocumentPart;
            var imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
            using (var ms = new MemoryStream())
            {
                img.Save(ms, ImageFormat.Jpeg);
                ms.Position = 0;
                imagePart.FeedData(ms);
            }
            AddImageToBody(mainPart.GetIdOfPart(imagePart));

            if (!string.IsNullOrEmpty(imageName))
            {
                var pIntro = _body.AppendChild(new Paragraph());
                pIntro.AppendChild(new Text(string.Format("图{0}. {1}\n", _imageCount, imageName)));
            }
        }

        #region Private methods

        private void AddImageToBody(string relationshipId)
        {
            var currId = _elementId++;
            var paragraph1 = GetImageParagraph(relationshipId, currId);
            _body.AppendChild(new Paragraph(new Run(paragraph1)));
        }

        private Paragraph GetImageParagraph(string relationshipId, UInt32Value currId)
        {
            var paragraph1 = new Paragraph { RsidParagraphAddition = "009C617C", RsidParagraphProperties = "00123672", RsidRunAdditionDefault = "00123672" };
            {
                var paragraphProperties1 = new ParagraphProperties();
                {
                    var justification1 = new Justification { Val = JustificationValues.Center };
                    paragraphProperties1.AppendChild(justification1);
                }
                var bookmarkStart1 = new BookmarkStart { Name = "_GoBack", Id = currId.ToString() };

                var run1 = new Run();
                {
                    var runProperties1 = new RunProperties();
                    {
                        var runFonts1 = new RunFonts { Hint = FontTypeHintValues.EastAsia };
                        var noProof1 = new NoProof();

                        runProperties1.AppendChild(runFonts1);
                        runProperties1.AppendChild(noProof1);

                    }
                    var drawing1 = new Drawing();
                    {
                        var inline1 = new DocumentFormat.OpenXml.Drawing.Wordprocessing.Inline
                        {
                            DistanceFromTop = 0U,
                            DistanceFromBottom = 0U,
                            DistanceFromLeft = 0U,
                            DistanceFromRight = 0U
                        };
                        {
                            var extent1 = new DocumentFormat.OpenXml.Drawing.Wordprocessing.Extent { Cx = 3781425L, Cy = 3781425L };
                            var effectExtent1 = new DocumentFormat.OpenXml.Drawing.Wordprocessing.EffectExtent
                            {
                                LeftEdge = 0L,
                                TopEdge = 0L,
                                RightEdge = 9525L,
                                BottomEdge = 9525L
                            };
                            var docProperties1 = new DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties { Id = currId, Name = "图片 " + currId };

                            var nonVisualGraphicFrameDrawingProperties1 = new DocumentFormat.OpenXml.Drawing.Wordprocessing.NonVisualGraphicFrameDrawingProperties();
                            {
                                var graphicFrameLocks1 = new DocumentFormat.OpenXml.Drawing.GraphicFrameLocks { NoChangeAspect = true };
                                graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
                                nonVisualGraphicFrameDrawingProperties1.AppendChild(graphicFrameLocks1);
                            }
                            var graphic1 = new DocumentFormat.OpenXml.Drawing.Graphic();
                            {

                                graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
                                var graphicData1 = new DocumentFormat.OpenXml.Drawing.GraphicData
                                {
                                    Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"
                                };
                                {
                                    var picture1 = new DocumentFormat.OpenXml.Drawing.Pictures.Picture();
                                    picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");

                                    var nonVisualPictureProperties1 = new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureProperties();
                                    var nonVisualDrawingProperties1 = new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualDrawingProperties { Id = currId, Name = "Picture " + currId };

                                    var nonVisualPictureDrawingProperties1 = new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureDrawingProperties();
                                    var pictureLocks1 = new DocumentFormat.OpenXml.Drawing.PictureLocks { NoChangeAspect = true, NoChangeArrowheads = true };

                                    nonVisualPictureDrawingProperties1.AppendChild(pictureLocks1);

                                    nonVisualPictureProperties1.AppendChild(nonVisualDrawingProperties1);
                                    nonVisualPictureProperties1.AppendChild(nonVisualPictureDrawingProperties1);

                                    var blipFill1 = new DocumentFormat.OpenXml.Drawing.Pictures.BlipFill();
                                    {
                                        var blip1 = new DocumentFormat.OpenXml.Drawing.Blip { Embed = relationshipId };
                                        {
                                            var blipExtensionList1 = new DocumentFormat.OpenXml.Drawing.BlipExtensionList();
                                            {
                                                var blipExtension1 = new DocumentFormat.OpenXml.Drawing.BlipExtension { Uri = Guid.NewGuid().ToString() };
                                                {
                                                    var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi { Val = false };
                                                    {
                                                        useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
                                                    }
                                                    blipExtension1.AppendChild(useLocalDpi1);
                                                }
                                                blipExtensionList1.AppendChild(blipExtension1);
                                            }
                                            blip1.AppendChild(blipExtensionList1);
                                        }
                                        var sourceRectangle1 = new DocumentFormat.OpenXml.Drawing.SourceRectangle();
                                        var stretch1 = new DocumentFormat.OpenXml.Drawing.Stretch();
                                        {
                                            var fillRectangle1 = new DocumentFormat.OpenXml.Drawing.FillRectangle();
                                            stretch1.AppendChild(fillRectangle1);
                                        }
                                        blipFill1.AppendChild(blip1);
                                        blipFill1.AppendChild(sourceRectangle1);
                                        blipFill1.AppendChild(stretch1);
                                    }
                                    var shapeProperties1 = new DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties { BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto };

                                    var transform2D1 = new DocumentFormat.OpenXml.Drawing.Transform2D();
                                    var offset1 = new DocumentFormat.OpenXml.Drawing.Offset { X = 0L, Y = 0L };
                                    var extents1 = new DocumentFormat.OpenXml.Drawing.Extents { Cx = 3781425L, Cy = 3781425L };

                                    transform2D1.AppendChild(offset1);
                                    transform2D1.AppendChild(extents1);

                                    var presetGeometry1 = new DocumentFormat.OpenXml.Drawing.PresetGeometry { Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle };
                                    var adjustValueList1 = new DocumentFormat.OpenXml.Drawing.AdjustValueList();

                                    presetGeometry1.AppendChild(adjustValueList1);
                                    var noFill1 = new DocumentFormat.OpenXml.Drawing.NoFill();

                                    var outline1 = new DocumentFormat.OpenXml.Drawing.Outline();
                                    var noFill2 = new DocumentFormat.OpenXml.Drawing.NoFill();

                                    outline1.AppendChild(noFill2);

                                    shapeProperties1.AppendChild(transform2D1);
                                    shapeProperties1.AppendChild(presetGeometry1);
                                    shapeProperties1.AppendChild(noFill1);
                                    shapeProperties1.AppendChild(outline1);

                                    picture1.AppendChild(nonVisualPictureProperties1);
                                    picture1.AppendChild(blipFill1);
                                    picture1.AppendChild(shapeProperties1);

                                    graphicData1.AppendChild(picture1);
                                }
                                graphic1.AppendChild(graphicData1);
                            }

                            inline1.AppendChild(extent1);
                            inline1.AppendChild(effectExtent1);
                            inline1.AppendChild(docProperties1);
                            inline1.AppendChild(nonVisualGraphicFrameDrawingProperties1);
                            inline1.AppendChild(graphic1);
                        }
                        drawing1.AppendChild(inline1);

                    }
                    run1.AppendChild(runProperties1);
                    run1.AppendChild(drawing1);
                    var bookmarkEnd1 = new BookmarkEnd { Id = currId.ToString() };

                    paragraph1.AppendChild(paragraphProperties1);
                    paragraph1.AppendChild(bookmarkStart1);
                    paragraph1.AppendChild(run1);
                    paragraph1.AppendChild(bookmarkEnd1);
                }
            }
            return paragraph1;
        }

        #endregion

    }
}
